package pers.fjl.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import pers.fjl.common.constant.MessageConstant;
import pers.fjl.common.entity.QueryPageBean;
import pers.fjl.common.po.Commodity;
import pers.fjl.common.po.Sales;
import pers.fjl.common.po.User;
import pers.fjl.common.vo.AddSales;
import pers.fjl.common.vo.SalesVo;
import pers.fjl.test.dao.SalesDao;
import pers.fjl.test.service.SalesService;
import org.springframework.cache.annotation.Cacheable;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

@Slf4j
@Service
public class SalesServiceImpl extends ServiceImpl<SalesDao, Sales> implements SalesService {
    @Resource
    private SalesDao salesDao;
    @Resource
    private SalesService salesService;

    @Cacheable(value = {"SalesMap"})
    public Page<SalesVo> findPage(QueryPageBean queryPageBean) {
        //设置分页条件
        Page<SalesVo> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //设置查询条件
        QueryWrapper<Sales> wrapper = new QueryWrapper<>();
        if (queryPageBean.getQueryString() != null) {  //不为空，则代表是根据用户名条件查询
            //查询总记录数
            page.setTotal(salesDao.selectSearchCount(queryPageBean.getQueryString()));
            page.setRecords(salesDao.searchByUsername(page, queryPageBean.getQueryString()));
            return page;
        }
        //执行全部查询
        page.setRecords(salesDao.getAllSales());
        //查询总记录数
        page.setTotal(salesDao.selectCount(null));
        return page;
    }

//    @Override
//    public boolean addSales(Sales sales) {
//        return false;
//    }

    @CacheEvict(value = {"SalesMap"}, allEntries = true)
    public boolean addSales(AddSales addsales) {
        QueryWrapper<AddSales> wrapper = new QueryWrapper<>();
//        log.info("addSales.sales.getUsername():[{}]", sales.getOid());
        User user = salesDao.getId(addsales.getUsername());
        Commodity commodity = salesDao.getCid(addsales.getCname());
        Sales sales = new Sales();
        sales.setCid(commodity.getCid());
        sales.setId(user.getId());
        sales.setCashierId(addsales.getCashierId());
        sales.setNumber(addsales.getNumber());
        Calendar calendar = Calendar.getInstance();
//        java.util.Date dt = new java.util.Date();
//        java.text.SimpleDateFormat sdf =
//                new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String currentTime = sdf.format(dt);
//        LocalDateTime localDateTime = new LocalDateTime(sdf);
        LocalDateTime localDateTime = LocalDateTime.now();
        sales.setDate(localDateTime);
//        sales.setOid(addsales.getOid());
        return salesService.save(sales);
    }
}
